package tests;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;


public class DBReaderTest {
	private Connection db;
	private int nOfCalls, currentCall;
	
	@Before
	public void setUp()  {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			//this.db=DriverManager.getConnection("jdbc:mysql://alarcosj.esi.uclm.es:3306/bancoupm", "alumnodis", "alumnodis");
			this.db=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bancoupm", "root", "");
			try {
				String SQL="Delete from Cuenta";
				PreparedStatement p=this.db.prepareStatement(SQL);
				p.executeUpdate();
			} catch (SQLException e) {
				System.out.println(e.toString());
				fail();
			}
			
			String SQL="Insert into Cuenta (numero, fecha, NIFTitular) values (1, '2013-07-31', '5660126X')";
			PreparedStatement p=this.db.prepareStatement(SQL);
			p.executeUpdate();
			for (int i=0; i<10; i++) {
				SQL="Insert into Movimiento (numeroDeCuenta, importe, fecha) values (1, 100, '2013-07-31')";
				p=this.db.prepareStatement(SQL);
				p.executeUpdate();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
//		try {
//			DataOutputStream dos=new DataOutputStream(new FileOutputStream("/private/var/folders/59/5rlw8d8s2p37hv3d7dz0q3qr0000gn/T/counter.counter"));
//			dos.writeInt(1);
//			dos.close();
//			this.currentCall=5;
//			DataInputStream dis=new DataInputStream(new FileInputStream("/var/folders/59/5rlw8d8s2p37hv3d7dz0q3qr0000gn/T/" +
//					"int java.io.InputStream.read(byte[], int, int).jp"));
//			this.nOfCalls=dis.readInt();
//			System.out.println(nOfCalls);
//			dis.close();
//		}
//		catch (Exception ex) {
//			ex.printStackTrace();
//		}
	}
	
	@After
	public void tearDown() {
		try {
			this.db.close();
		} catch (SQLException e) {
			fail(e.toString());
		}
	}

	@Test
	public void testGetNumberOfOperations() {
		try {
			int nom=0;
			String SQL="Select count(*) from Movimiento";
			PreparedStatement p=this.db.prepareStatement(SQL);
			ResultSet r=p.executeQuery();
			if (r.next()) { 
				nom=r.getInt(1);
			}
			//System.out.println("nom=" + nom);
			assertTrue("Expected 10; obtained=" + nom, nom==10);
			SQL="Insert into Cuenta (numero, fecha, nifTitular) values (?, CurDate(), '50005')";
			PreparedStatement p2=this.db.prepareStatement(SQL);
			p2.setInt(1, 10000);
			p2.executeUpdate();
		} catch (Exception e) {
			fail("Unexpected exception: " + e.toString());
		}
	}

	@Test
	public void testGetAmounts() {
		try {
			Vector<Double> amounts=new Vector<Double>();
			String SQL="Select Importe from Movimiento";
			PreparedStatement p=this.db.prepareStatement(SQL);
			ResultSet r=p.executeQuery();
			while (r.next()) {
				amounts.add(r.getDouble(1));
			}
//			System.out.println("amounts.size()=" + amounts.size());
			assertTrue("Expected 10; obtained " + amounts.size(), amounts.size()==10);
		} catch (Exception e) {
			fail("Unexpected exception: " + e.toString());
		}
	}


}
